# There should be an entry in this list for every .bnf in this directory (omit
# the .bnf suffix).
FILES = [
    "abort_stmt",
    "add_column",
    "add_constraint",
    "alter_changefeed",
    "alter_backup",
    "alter_backup_schedule",
    "alter_column",
    "alter_database",
    "alter_database_add_region_stmt",
    "alter_database_add_super_region",
    "alter_database_set_secondary_region_stmt",
    "alter_database_alter_super_region",
    "alter_database_drop_super_region",
    "alter_database_drop_region",
    "alter_database_drop_secondary_region",
    "alter_database_owner",
    "alter_database_placement_stmt",
    "alter_database_primary_region",
    "alter_database_set_zone_config_extension_stmt",
    "alter_database_set_stmt",
    "alter_database_survival_goal_stmt",
    "alter_database_to_schema_stmt",
    "alter_ddl_stmt",
    "alter_default_privileges_stmt",
    "alter_func_stmt",
    "alter_func_options_stmt",
    "alter_func_rename_stmt",
    "alter_func_owner_stmt",
    "alter_func_set_schema_stmt",
    "alter_func_dep_extension_stmt",
    "alter_proc",
    "alter_proc_rename_stmt",
    "alter_proc_owner_stmt",
    "alter_proc_set_schema_stmt",
    "alter_index_partition_by",
    "alter_index",
    "alter_index_visible_stmt",
    "alter_partition_stmt",
    "alter_primary_key",
    "alter_range_relocate_stmt",
    "alter_range",
    "alter_rename_view_stmt",
    "alter_role_stmt",
    "alter_scatter_index_stmt",
    "alter_scatter_stmt",
    "alter_schema",
    "alter_sequence",
    "alter_sequence_options_stmt",
    "alter_sequence_owner_stmt",
    "alter_sequence_set_schema_stmt",
    "alter_stmt",
    "alter_table",
    "alter_table_cmds",
    "alter_table_locality_stmt",
    "alter_table_owner_stmt",
    "alter_table_partition_by",
    "alter_table_reset_storage_param",
    "alter_table_set_schema_stmt",
    "alter_table_set_storage_param",
    "alter_type",
    "alter_view",
    "alter_view_owner_stmt",
    "alter_view_set_schema_stmt",
    "alter_zone_database_stmt",
    "alter_zone_index_stmt",
    "alter_zone_partition_stmt",
    "alter_zone_range_stmt",
    "alter_zone_table_stmt",
    "analyze_stmt",
    "backup",
    "backup_options",
    "begin_stmt",
    "begin_transaction",
    "call",
    "cancel_all_jobs",
    "cancel_job",
    "cancel_query",
    "cancel_session",
    "cancel_stmt",
    "check_column_level",
    "check_table_level",
    "close_cursor_stmt",
    "col_qualification",
    "column_table_def",
    "comment",
    "commit_transaction",
    "copy_stmt",
    "copy_to_stmt",
    "create_as_col_qual_list",
    "create_as_constraint_def",
    "create_changefeed_stmt",
    "create_database_stmt",
    "create_ddl_stmt",
    "create_extension_stmt",
    "create_external_connection_stmt",
    "create_func",
    "create_index_stmt",
    "create_index_with_storage_param",
    "create_inverted_index_stmt",
    "create_proc",
    "create_role_stmt",
    "create_schedule_for_backup_stmt",
    "create_schedule_for_changefeed_stmt",
    "create_schedule_stmt",
    "create_schema_stmt",
    "create_sequence_stmt",
    "create_stats_stmt",
    "create_stmt",
    "create_table_as_stmt",
    "create_table_with_storage_param",
    "create_table_stmt",
    "create_trigger_stmt",
    "create_type",
    "create_view_stmt",
    "deallocate_stmt",
    "declare_cursor_stmt",
    "default_value_column_level",
    "delete_stmt",
    "discard_stmt",
    "drop_column",
    "drop_constraint",
    "drop_database",
    "drop_ddl_stmt",
    "drop_external_connection_stmt",
    "drop_func_stmt",
    "drop_proc",
    "drop_index",
    "drop_owned_by_stmt",
    "drop_role_stmt",
    "drop_schedule_stmt",
    "drop_schema",
    "drop_sequence_stmt",
    "drop_stmt",
    "drop_table",
    "drop_trigger_stmt",
    "drop_type",
    "drop_view",
    "execute_stmt",
    "experimental_audit",
    "explain_analyze_stmt",
    "explain_stmt",
    "explainable_stmt",
    "export_stmt",
    "family_def",
    "fetch_cursor_stmt",
    "fingerprint_options",
    "fingerprint_options_list",
    "for_locking",
    "foreign_key_column_level",
    "foreign_key_table_level",
    "generic_set",
    "grant_stmt",
    "import_csv",
    "import_dump",
    "import_into",
    "index_def",
    "insert_rest",
    "insert_stmt",
    "iso_level",
    "joined_table",
    "legacy_begin_stmt",
    "legacy_end_stmt",
    "legacy_transaction_stmt",
    "like_table_option_list",
    "limit_clause",
    "move_cursor_stmt",
    "not_null_column_level",
    "offset_clause",
    "on_conflict",
    "opt_frame_clause",
    "opt_locality",
    "opt_persistence_temp_table",
    "opt_with_show_fingerprints_options",
    "opt_with_storage_parameter_list",
    "pause_all_jobs",
    "pause_job",
    "pause_schedule",
    "pause_stmt",
    "preparable_stmt",
    "prepare_stmt",
    "primary_key_column_level",
    "primary_key_table_level",
    "reassign_owned_by_stmt",
    "refresh_materialized_views",
    "release_savepoint",
    "rename_column",
    "rename_constraint",
    "rename_database",
    "rename_index",
    "rename_sequence",
    "rename_table",
    "reset_csetting_stmt",
    "reset_session_stmt",
    "reset_stmt",
    "restore",
    "restore_options",
    "resume_all_jobs",
    "resume_job",
    "resume_schedule",
    "resume_stmt",
    "revoke_stmt",
    "rollback_transaction",
    "routine_body_stmt",
    "routine_return_stmt",
    "row_source_extension_stmt",
    "savepoint_stmt",
    "scrub_database_stmt",
    "scrub_stmt",
    "scrub_table_stmt",
    "select_clause",
    "select_stmt",
    "set_cluster_setting",
    "set_csetting_stmt",
    "set_or_reset_csetting_stmt",
    "set_exprs_internal",
    "set_local_stmt",
    "set_operation",
    "set_rest",
    "set_rest_more",
    "set_session_stmt",
    "set_transaction",
    "set_transaction_stmt",
    "show_backup",
    "show_cluster_setting",
    "show_columns_stmt",
    "show_commit_timestamp_stmt",
    "show_constraints_stmt",
    "show_create_stmt",
    "show_create_schedules_stmt",
    "show_create_external_connections_stmt",
    "show_external_connections_stmt",
    "show_databases_stmt",
    "show_default_privileges_stmt",
    "show_enums",
    "show_full_scans",
    "show_functions_stmt",
    "show_procedures_stmt",
    "show_grants_stmt",
    "show_indexes_stmt",
    "show_jobs",
    "show_keys",
    "show_locality",
    "show_locality_stmt",
    "show_partitions_stmt",
    "show_range_for_row_stmt",
    "show_ranges_stmt",
    "show_regions",
    "show_roles_stmt",
    "show_savepoint_status",
    "show_schedules",
    "show_schemas",
    "show_sequences",
    "show_session_stmt",
    "show_sessions",
    "show_statements",
    "show_stats",
    "show_survival_goal_stmt",
    "show_tables",
    "show_trace",
    "show_transactions_stmt",
    "show_transfer_stmt",
    "show_types_stmt",
    "show_users_stmt",
    "show_default_session_variables_for_role_stmt",
    "show_var",
    "show_zone_stmt",
    "simple_select_clause",
    "sort_clause",
    "split_index_at",
    "split_table_at",
    "stmt",
    "stmt_block",
    "stmt_without_legacy_transaction",
    "table_clause",
    "table_constraint",
    "table_ref",
    "transaction_stmt",
    "truncate_stmt",
    "unique_column_level",
    "unique_table_level",
    "unsplit_index_at",
    "unsplit_table_at",
    "update_stmt",
    "upsert_stmt",
    "use_stmt",
    "validate_constraint",
    "values_clause",
    "window_definition",
    "with_clause",
    "unlisten_stmt",
]

genrule(
    name = "bnf",
    srcs = ["//pkg/sql/parser:sql.y"],
    outs = [file + ".bnf" for file in FILES],
    cmd = """
    $(location //pkg/cmd/docgen) grammar bnf $(RULEDIR) --quiet --addr $(location //pkg/sql/parser:sql.y)
    for FILE in $$(ls $(RULEDIR))
    do
        if [[ "$(OUTS)" != *"$$FILE"* ]]; then
            echo "$$FILE is not a generated file; please add $${FILE%.bnf} to the list of FILES in docs/generated/sql/bnf/BUILD.bazel"
            exit 1
        fi
    done
    """,
    tools = ["//pkg/cmd/docgen"],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)

genrule(
    name = "svg",
    srcs = [
        "@railroadjar//:rr.war",
        "//pkg/sql/parser:sql.y",
        ":bnf",
    ],
    outs = [file.replace("_stmt", "") + ".html" for file in FILES if "_stmt" not in file or file.replace("_stmt", "") not in FILES],
    cmd = """
    bnf_locs=$$(echo $(locations :bnf) | cut -d' ' -f1 | xargs dirname)
    COCKROACH_REQUIRE_RAILROAD=1 $(location //pkg/cmd/docgen) grammar svg $$bnf_locs $(RULEDIR) --addr $(location //pkg/sql/parser:sql.y) --railroad $(location @railroadjar//:rr.war)
    """,
    tools = ["//pkg/cmd/docgen"],
    visibility = [
        ":__pkg__",
        "//pkg/gen:__pkg__",
    ],
)
